import { ref } from "vue";
import { defineStore } from "pinia";
import type { menu } from "~/types/menu";
export const usePageTabsStore = defineStore(
  "pageTabs",
  () => {
    const pageTabs = ref<menu[]>([]);
    const current_pageTab = ref("");

    function addTab(newTab: menu) {
      if (pageTabs.value.some((tab) => tab.path === newTab.path)) return;

      pageTabs.value.push(newTab);
    }

    function delTab(targetPath: string) {
      pageTabs.value = pageTabs.value.filter((tab) => tab.path !== targetPath);
    }

    function closeOtherTabs(targetPath: string) {
      pageTabs.value = pageTabs.value.filter((tab) => tab.path === targetPath);
    }

    function closeAllTabs() {
      pageTabs.value = [];
      current_pageTab.value = "";
    }

    return {
      pageTabs,
      current_pageTab,
      addTab,
      delTab,
      closeOtherTabs,
      closeAllTabs,
    };
  },
  {
    // 默认持久化在 cookie 中
    persist: true,
  }
);
